package ntut.andengine.game;

import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameLevel3 extends GameLevel {

	GameLevel mLevel;
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		mLevel = this;
		
		
		BitmapTexture backgroundBitmapTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion backgroundTextureRegion = BitmapTextureRegionFactory.createFromAsset(backgroundBitmapTexture, this, "universe.png", 0, 0);
		mEngine.getTextureManager().loadTexture(backgroundBitmapTexture);
		
		Sprite background = new Sprite(0, 0, backgroundTextureRegion);
		mScene.attachChild(background);
	}
	
	@Override
	public Scene onLoadScene() {
		
		PhysicalFactory.CreateWall(this, new Vector2(238, 142), 140, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(73, 329), 20, 540, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(332, 729), 180, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(158, 260), 20, 520, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(349, 660), 140, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(232, 669), 20, 140, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(193, 589), 260, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(232, 349), 20, 140, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(328, 510), 320, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(310, 249), 20, 380, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(194, 429), 420, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(333, 650), 20, 300, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(390, 350), 180, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(392, 180), 20, 360, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(322, 270), 160, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(412, 590), 20, 140, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(223, 69), 320, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(193, 69), 220, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(412, 650), 20, 300, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(282, 270), 240, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(392, 130), 20, 260, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(391, 350), 180, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(310, 130), 20, 260, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(372, 429), 300, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(232, 380), 20, 240, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(328, 510), 360, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(232, 669), 20, 140, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(203, 589), 280, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(158, 482), 20, 700, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(254, 660), 520, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(73, 329), 20, 540, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(362, 729), 240, 20, ComponentColor.Red); 

        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(441, 120));
        gate.getBody().setUserData(new ContactEvent("Gate"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				
				// LEVEL COMPLETE!!
				if(body.getUserData().toString() == "Player"){
					mLevel.runOnUiThread(new Runnable(){

						@Override
						public void run() {
							mMusic.pause();
							successSound.play();
							Toast.makeText(mLevel, "Level Completed!", Toast.LENGTH_SHORT).show();	
						}
			        	
			        });
					startActivity(GameLevel3.this, GameLevel5.class);
				}
			
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {
				
			}
		
		});
        
        mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(75, 733), ComponentColor.Blue);
	
		this.mScene.registerUpdateHandler(this.mPhysicsWorld);	

		return mScene;
	}

}
